package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.SopCytologyPathologyMapper;
import com.zhijian.medical.entity.po.SopCytologyPathology;
import com.zhijian.medical.entity.vo.request.SopCytologyPathologySaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.SopCytologyPathologySearchVo;
import com.zhijian.medical.entity.vo.response.SopCytologyPathologyVo;
import com.zhijian.medical.service.SopCytologyPathologyService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * SOP-签发细胞学病理报告资质考核授权业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 17:20:35
 */
@Slf4j
@Service
public class SopCytologyPathologyServiceImpl implements SopCytologyPathologyService {

	@Autowired
    private SopCytologyPathologyMapper mapper;

    @Override
    public SopCytologyPathology selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public SopCytologyPathologyVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(SopCytologyPathology sopCytologyPathology) {
        Date now = new Date();
        sopCytologyPathology.setCreateTime(now);
        sopCytologyPathology.setUpdateTime(now);
        return mapper.insert(sopCytologyPathology) > 0 ? sopCytologyPathology.getId() : 0L;
    }

    @Override
    public Long insertSelective(SopCytologyPathology sopCytologyPathology) {
        Date now = new Date();
        sopCytologyPathology.setCreateTime(now);
        sopCytologyPathology.setUpdateTime(now);
        return mapper.insertSelective(sopCytologyPathology) > 0 ? sopCytologyPathology.getId() : 0L;
    }

    @Override
    public boolean update(SopCytologyPathology sopCytologyPathology) {
        Date now = new Date();
        sopCytologyPathology.setUpdateTime(now);
        return mapper.updateByPrimaryKey(sopCytologyPathology) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(SopCytologyPathology record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(SopCytologyPathologySaveOrUpdateVo saveOrUpdateVo) {
        SopCytologyPathology sopCytologyPathology = new SopCytologyPathology();
        BeanUtils.copyProperties(saveOrUpdateVo, sopCytologyPathology);
        return this.insert(sopCytologyPathology) != 0;
    }

    @Override
    public boolean modify(SopCytologyPathologySaveOrUpdateVo saveOrUpdateVo) {
        SopCytologyPathology old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        SopCytologyPathology sopCytologyPathology = new SopCytologyPathology();
        BeanUtils.copyProperties(saveOrUpdateVo, sopCytologyPathology);
        return this.update(sopCytologyPathology);
    }

    @Override
    public SopCytologyPathologyVo selectOne(SopCytologyPathologySearchVo sopCytologyPathologySearchVo) {
        return mapper.selectOne(sopCytologyPathologySearchVo);
     }

    @Override
    public Integer count(SopCytologyPathologySearchVo sopCytologyPathologySearchVo) {
        return mapper.count(sopCytologyPathologySearchVo);
    }

    @Override
    public List<SopCytologyPathologyVo> selectList(SopCytologyPathologySearchVo sopCytologyPathologySearchVo) {
        return mapper.selectList(sopCytologyPathologySearchVo);
    }

    @Override
    public Page<SopCytologyPathologyVo> selectPage(SopCytologyPathologySearchVo sopCytologyPathologySearchVo) {
        AssertUtil.check(Objects.isNull(sopCytologyPathologySearchVo.getPageNum()) || Objects.isNull(sopCytologyPathologySearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(sopCytologyPathologySearchVo);
        //分页
        Page<SopCytologyPathologyVo> page = new Page<>(true, count, sopCytologyPathologySearchVo.getPageNum(), sopCytologyPathologySearchVo.getPageSize());
        sopCytologyPathologySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<SopCytologyPathologyVo> responseVos = this.selectList(sopCytologyPathologySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<SopCytologyPathology> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(SopCytologyPathologySearchVo sopCytologyPathologySearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(sopCytologyPathologySearchVo);
    }
    
}